<template>
  <div class="app-container">


    <el-form :inline="true" :model="menuData" class="demo-form-inline" size="small">
      <el-form-item label="菜单名称">
        <el-input v-model="menuData.menuName" placeholder="请输入菜单名称"></el-input>
      </el-form-item>
      <el-form-item label="状态">
        <el-select v-model="menuData.status" placeholder="菜单状态">
          <el-option label="正常" value="0"></el-option>
          <el-option label="停用" value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="mini" icon="el-icon-search" @click="list">查询</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

<!--    新增在左边-->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['system:menu:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="info"
            plain
            icon="el-icon-sort"
            size="mini"
            @click="toggleExpandAll"
        >展开/折叠</el-button>
      </el-col>
<!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="list"></right-toolbar>-->
    </el-row>

    <el-table
        v-if="refreshTable"
        :data="tableData"
        style="width: 100%;margin-bottom: 20px;"
        row-key="id"
        border
        :default-expand-all="isExpandAll"
        :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
<!--      <el-table-column-->
<!--          prop="id"-->
<!--          label="id"-->
<!--          sortable-->
<!--          >-->
<!--      </el-table-column>-->
      <el-table-column
          prop="menuName"
          label="菜单名称"
          sortable
          >
      </el-table-column>
      <el-table-column
          prop="orderNum"
          label="排序"
          sortable
          >
      </el-table-column>
      <el-table-column prop="icon" label="图标" align="center" width="100">
        <template slot-scope="scope">
          <span :class="scope.row.icon"></span>
        </template>
      </el-table-column>
      <el-table-column
          prop="perms"
          label="权限"
          sortable
          >
      </el-table-column>
      <el-table-column
          prop="component"
          label="组件路径"
          sortable
          >
      </el-table-column>

      <el-table-column
          prop="status"
          label="状态"
          sortable
          >
        <template v-slot="s">
          <span v-if="s.row.status==0"> <el-button type="primary" plain>正常</el-button></span>
          <span v-if="s.row.status==1"> <el-button type="primary" plain>停止</el-button></span>
        </template>
      </el-table-column>
      <el-table-column
          prop="createTime"
          label="时间"
          align="center"
          sortable
          >
        <template slot-scope="scope">
          <span>{{ (scope.row.createTime.replace("T"," ")) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['system:menu:edit']"
          >修改</el-button>
          <el-button
              size="mini"
              type="text"
              icon="el-icon-plus"
              @click="handleAdd(scope.row)"
              v-hasPermi="['system:menu:add']"
          >新增</el-button>
          <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['system:menu:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>


    <!-- 添加或修改菜单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="680px" append-to-body>
      <el-form ref="form" :model="form"  label-width="100px">
        <el-row>
<!--          联动-->
          <el-col :span="24">
            <el-form-item label="上级菜单" prop="parentId" v-model="form.parentId">
              <el-cascader
                  v-model="value"
                  :options="menuOptions"
                  :props="props"
                  @change="handleChange"></el-cascader>
            </el-form-item>
          </el-col>
<!--          菜单类型-->
          <el-col :span="24">
            <el-form-item label="菜单类型" prop="menuType">
              <el-radio-group v-model="form.menuType">
                <el-radio label="M">目录</el-radio>
                <el-radio label="C">菜单</el-radio>
                <el-radio label="F">按钮</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>

<!--         输入 菜单名称-->
          <el-col :span="12">
            <el-form-item label="菜单名称" prop="menuName">
              <el-input v-model="form.menuName" placeholder="请输入菜单名称" />
            </el-form-item>
          </el-col>
<!--          显示排序-->
          <el-col :span="12">
            <el-form-item label="显示排序" prop="orderNum">
              <el-input-number v-model="form.orderNum" controls-position="right" :min="0" />
            </el-form-item>
          </el-col>
<!--          是否外链-->
          <el-col :span="12" v-if="form.menuType != 'F'">
            <el-form-item prop="isFrame">
              <span slot="label">
                <el-tooltip content="选择是外链则路由地址需要以`http(s)://`开头" placement="top">
                <i class="el-icon-question"></i>
                </el-tooltip>
                是否外链
              </span>
              <el-radio-group v-model="form.isFrame">
                <el-radio label="0">是</el-radio>
                <el-radio label="1">否</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
<!--          路由地址-->
          <el-col :span="12" v-if="form.menuType != 'F'">
            <el-form-item prop="path">
              <span slot="label">
                <el-tooltip content="访问的路由地址，如：`user`，如外网地址需内链访问则以`http(s)://`开头" placement="top">
                <i class="el-icon-question"></i>
                </el-tooltip>
                路由地址
              </span>
              <el-input v-model="form.path" placeholder="请输入路由地址" />
            </el-form-item>
          </el-col>
<!--          组件路径-->
          <el-col :span="12" v-if="form.menuType == 'C'">
            <el-form-item prop="component">
              <span slot="label">
                <el-tooltip content="访问的组件路径，如：`system/user/index`，默认在`views`目录下" placement="top">
                <i class="el-icon-question"></i>
                </el-tooltip>
                组件路径
              </span>
              <el-input v-model="form.component" placeholder="请输入组件路径" />
            </el-form-item>
          </el-col>
<!--          权限字符-->
          <el-col :span="12" v-if="form.menuType != 'M'">
            <el-form-item prop="perms">
              <el-input v-model="form.perms" placeholder="请输入权限标识" maxlength="100" />
              <span slot="label">
                <el-tooltip content="控制器中定义的权限字符，如：@PreAuthorize(`@ss.hasPermi('system:user:list')`)" placement="top">
                <i class="el-icon-question"></i>
                </el-tooltip>
                权限字符
              </span>
            </el-form-item>
          </el-col>
<!--          路由参数-->
          <el-col :span="12" v-if="form.menuType == 'C'">
            <el-form-item prop="query">
              <el-input v-model="form.query" placeholder="请输入路由参数" maxlength="255" />
              <span slot="label">
                <el-tooltip content='访问路由的默认传递参数，如：`{"id": 1, "name": "ry"}`' placement="top">
                <i class="el-icon-question"></i>
                </el-tooltip>
                路由参数
              </span>
            </el-form-item>
          </el-col>
<!--          是否缓存-->
          <el-col :span="12" v-if="form.menuType == 'C'">
            <el-form-item prop="isCache">
              <span slot="label">
                <el-tooltip content="选择是则会被`keep-alive`缓存，需要匹配组件的`name`和地址保持一致" placement="top">
                <i class="el-icon-question"></i>
                </el-tooltip>
                是否缓存
              </span>
              <el-radio-group v-model="form.isCache">
                <el-radio label="0">缓存</el-radio>
                <el-radio label="1">不缓存</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
<!--          显示状态-->
          <el-col :span="12" v-if="form.menuType != 'F'">
            <el-form-item prop="visible">
              <span slot="label">
                <el-tooltip content="选择隐藏则路由将不会出现在侧边栏，但仍然可以访问" placement="top">
                <i class="el-icon-question"></i>
                </el-tooltip>
                显示状态
              </span>
              <el-radio-group v-model="form.visible">
                <el-radio  label="0">显示</el-radio>
                <el-radio  label="1">隐藏</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
<!--          菜单状态-->
          <el-col :span="12">
            <el-form-item prop="status">
              <span slot="label">
                <el-tooltip content="选择停用则路由将不会出现在侧边栏，也不能被访问" placement="top">
                <i class="el-icon-question"></i>
                </el-tooltip>
                菜单状态
              </span>
              <el-radio-group v-model="form.status">
                <el-radio  label="0">正常</el-radio>
                <el-radio  label="1">停用</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>

        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
export default {
  name: "MenuVIew",
  data() {
    return {
      type:0,
      menuData:{},
      tableData:[],
      isExpandAll:false,
      refreshTable:true,
      props:{
        value:'id',
        label:'menuName',
        checkStrictly: true,
      },
      value: [],
      title:'',
      open:false,
      form:{},
      menuOptions:[],

    }
  }, methods: {
    handleDelete(row){
      this.axios.post("http://localhost:8000/carsystem/menu/handleDelete?id="+row.id).then(()=>{
        this.$message.success("删除成功")
        this.list();
      })
    },
    handleUpdate(row){
      this.axios.post("http://localhost:8000/carsystem/menu/handleUpdate?id="+row.id).then(res=>{
        console.log(res.data)
        // this.value=[res.data.data.parentId+''];
        this.value=res.data.values;
        this.form.parentId=res.data.data.parentId
        this.form=res.data.data;
        this.title='修改菜单';
        this.open=true;
        this.type=1;
      })
    },
    submitForm(){
      console.log(this.type)
      if (this.type==0){
        this.form.parentId=this.value[this.value.length-1]
        this.axios.post("http://localhost:8000/carsystem/menu/addMenu",this.form).then(()=>{
          console.log(this.form)
          this.$message.success("添加成功")
          this.open=false;
          this.list();
        })
      } else if (this.type==1){
        this.axios.post("http://localhost:8000/carsystem/menu/updateMenu",this.form).then(()=>{
          console.log(this.form)
          this.$message.success("修改成功")
          this.open=false;
          this.list();
        })
      }

    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.listDao()
    },
    handleChange(value) {
      console.log(value);
      console.log(value[value.length-1])
    },
    toggleExpandAll(){
      this.refreshTable=false;
      this.isExpandAll=!this.isExpandAll;
      this.$nextTick(() => {
        this.refreshTable = true;
      });
    },
    resetQuery(){
      this.menuData={};
      this.list();
    },
    handleAdd(){
      this.form={}
      this.value=[]
      this.type=0;
      this.open=true;
      this.title='添加菜单';
    },
    listDao(){
      this.axios.post("http://localhost:8000/carsystem/menu/listDao").then(res=>{
        this.menuOptions=res.data;
      })
    },
    list(){
      this.axios.post("http://localhost:8000/carsystem/menu/list",this.menuData).then(res=>{
        this.tableData=res.data;
      })
    }
  }, created() {
    this.list();
    this.listDao()
  }

}
</script>

<style scoped>
.app-container {
  padding: 20px;
}
</style>